package org.jackysoft.service.impl;

import java.io.File;

import org.jackysoft.entity.OfficeTemplate;
import org.jackysoft.service.AbstractIdentifiedService;
import org.jackysoft.service.OfficeTemplateService;
import org.jackysoft.util.AttachmentType;
import org.jackysoft.util.IOUtils;
import org.jackysoft.util.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service@Transactional
public class OfficeTemplateServiceImpl 
extends AbstractIdentifiedService<OfficeTemplate> implements
		OfficeTemplateService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 72197323370086580L;

	
	public OfficeTemplateServiceImpl() {
		this.type = OfficeTemplate.class;
	}

		

	@Override
	@Transactional
	public void create(File file,String fileName)
			throws DataAccessException {
		
		OfficeTemplate t = new OfficeTemplate();
	    t.setType(AttachmentType.getType(StringUtils.splitPostFileName(fileName)));
		t.setName(fileName);
		t.setData(IOUtils.toByteArray(file));
		t.setExt(StringUtils.splitPostFileName(fileName));
        super.create(t);
	}

	@Override
	public void update(OfficeTemplate otl, File temp ,String fileName)
			throws DataAccessException {
		OfficeTemplate t =get(otl.getId());
		t.setType(AttachmentType.getType(StringUtils.splitPostFileName(fileName)));
		t.setName(StringUtils.splitPreFileName(fileName));
		t.setData(IOUtils.toByteArray(temp));	
		super.update(t);
	}

}
